<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="部门名称" prop="departmentName">
      <el-input v-model="dataForm.departmentName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="上级部门" prop="parentName">
      <!-- <el-input v-model="dataForm.parentId" placeholder="上级部门"></el-input> -->
      <el-popover
          ref="departListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="departList"
            :props="departListTreeProps"
            node-key="id"
            ref="departListTree"
            @current-change="departListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:departListPopover :readonly="true" placeholder="点击选择上级部门" class="depart-list__input"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建日期" prop="createTime">
    
      <el-date-picker v-model="dataForm.createTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                                    required placeholder="创建日期"></el-date-picker>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          departmentName: '',
          parentId: '',
          createTime: '',
          parentName: ''
        },
        departList: [],
        departListTreeProps: {
          label: 'departmentName',
          children: 'children'
        },
        dataRule: {
          departmentName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        // console.log(JSON.stringify(this.dataForm))
        this.$http({
          url: this.$http.adornUrl('/business/department/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.departList = treeDataTranslate(data.data, 'id')
          // console.log(JSON.stringify(this.departList))
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (!this.dataForm.id) {
            // 新增
            this.departListTreeSetCurrentNode()
          } else {
            // 修改
            this.$http({
              url: this.$http.adornUrl(`/business/department/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              console.log(JSON.stringify(data))
              this.dataForm.id = data.businessDepartment.id
              this.dataForm.departmentName = data.businessDepartment.departmentName
              this.dataForm.parentId = data.businessDepartment.parentId
              this.dataForm.createTime = data.businessDepartment.createTime
              this.departListTreeSetCurrentNode()
            })
          }
        })
      },
      // 菜单树选中
      departListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.departmentName
      },
      // 菜单树设置当前选中节点
      departListTreeSetCurrentNode () {
        this.$refs.departListTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.departListTree.getCurrentNode() || {})['departmentName']
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/department/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'departmentName': this.dataForm.departmentName,
                'parentId': this.dataForm.parentId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
